package xyz.yuzegod.megawalls.game;

import net.minecraft.server.v1_8_R3.DamageSource;
import net.minecraft.server.v1_8_R3.Entity;
import net.minecraft.server.v1_8_R3.World;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.entity.Player;
import org.bukkit.entity.Wither;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.config.FileConfig;
import xyz.yuzegod.megawalls.game.team.TeamColor;
import xyz.yuzegod.megawalls.game.team.TeamWither;
import xyz.yuzegod.megawalls.util.EntityTypes;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.List;

public class GameTeam
{
    private List<GamePlayer> gamePlayers;
    private TeamColor teamColor;
    private Location spawnLocation;
    private Location witherLocation;
    private GameRegion region;
    private GameRegion castleRegion;
    private GameWall reserveWall;
    private int maxPlayers;
    private TeamWither teamWither;
    private boolean witherDead;
    
    private GameTeam() {
        this.gamePlayers = new ArrayList<GamePlayer>();
        this.teamColor = null;
        this.spawnLocation = null;
        this.witherLocation = null;
        this.region = null;
        this.castleRegion = null;
        this.reserveWall = null;
        this.maxPlayers = 0;
        this.teamWither = null;
        this.witherDead = false;
    }
    
    public static GameTeam build(final FileConfig config, final TeamColor teamColor, final CommandSender sender) {
        GameTeam gameTeam = null;
        try {
            gameTeam = new GameTeam();
            gameTeam.teamColor = teamColor;
            gameTeam.maxPlayers = MegaWalls.getInstance().getConfig().getInt("team-size", 20);
            gameTeam.spawnLocation = config.getLocation("teams." + teamColor.name() + ".spawn");
            gameTeam.witherLocation = config.getLocation("teams." + teamColor.name() + ".wither");
            gameTeam.region = new GameRegion(config.getBlockLocation("teams." + teamColor.name() + ".region.loc1"), config.getBlockLocation("teams." + teamColor.name() + ".region.loc2"));
            gameTeam.castleRegion = new GameRegion(config.getBlockLocation("teams." + teamColor.name() + ".castle.loc1"), config.getBlockLocation("teams." + teamColor.name() + ".castle.loc2"));
            gameTeam.reserveWall = new GameWall(config.getBlockLocation("teams." + teamColor.name() + ".reserve.loc1"), config.getBlockLocation("teams." + teamColor.name() + ".reserve.loc2"));
        }
        catch (Exception e) {
            sender.sendMessage("§c加载队伍时出错: §b" + e.getMessage());
        }
        return gameTeam;
    }
    
    public void spawnWither() {
        if (this.teamWither != null) {
            return;
        }
        (this.teamWither = new TeamWither((World)((CraftWorld)Bukkit.getWorld("world")).getHandle())).setTeam(this);
        final Game game = MegaWalls.getInstance().getGame();
        final double maxHealth = MegaWalls.getInstance().getConfig().getDouble("wither-health", 500.0);
        ((Wither)this.teamWither.getBukkitEntity()).setMaxHealth(maxHealth);
        ((Wither)this.teamWither.getBukkitEntity()).setHealth(maxHealth);
        EntityTypes.spawnEntity((Entity)this.teamWither, this.witherLocation);
        Bukkit.getScheduler().runTaskTimer((Plugin)MegaWalls.getInstance(), (Runnable)new Runnable() {
            private int second = 0;
            
            @Override
            public void run() {
                if (!GameTeam.this.isWitherDead()) {
                    if (this.second == 5) {
                        if (GameTeam.this.teamWither.getHealth() >= 3.0f && game.isWallsFall()) {
                            GameTeam.this.teamWither.damageEntity(DamageSource.GENERIC, 2.0f);
                        }
                        for (final Player other : PlayerUtils.getNearbyPlayers(GameTeam.this.witherLocation, 10.0)) {
                            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
                            if (!gameOther.isSpectator()) {
                                if (!GameTeam.this.isInTeam(gameOther)) {
                                    continue;
                                }
                                other.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 40, 0));
                                other.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 40, 0));
                            }
                        }
                        this.second = 0;
                    }
                    if (MegaWalls.getInstance().getGame().isWitherAngry()) {
                        GameTeam.this.teamWither.setCustomName(GameTeam.this.getTeamColor().getChatColor() + GameTeam.this.getTeamColor().getText() + "凋零 §l暴怒中");
                    }
                    else {
                        GameTeam.this.teamWither.setCustomName(GameTeam.this.getTeamColor().getChatColor() + GameTeam.this.getTeamColor().getText() + " §l凋零");
                    }
                }
                ++this.second;
            }
        }, 0L, 20L);
    }
    
    public List<GamePlayer> getAlivePlayers() {
        final List<GamePlayer> alivePlayers = new ArrayList<GamePlayer>();
        for (final GamePlayer gamePlayer : this.gamePlayers) {
            if (gamePlayer.isOnline() && !gamePlayer.isSpectator()) {
                alivePlayers.add(gamePlayer);
            }
        }
        return alivePlayers;
    }
    
    public boolean isInTeam(final GamePlayer gamePlayer) {
        for (final GamePlayer player : this.gamePlayers) {
            if (player.equals(gamePlayer)) {
                return true;
            }
        }
        return false;
    }
    
    public boolean addPlayer(final GamePlayer gamePlayer) {
        if (this.isFull() || this.isInTeam(gamePlayer)) {
            return false;
        }
        this.gamePlayers.add(gamePlayer);
        gamePlayer.setGameTeam(this);
        return true;
    }
    
    public boolean isFull() {
        return this.gamePlayers.size() >= this.getMaxPlayers();
    }
    
    public boolean isDead() {
        for (final GamePlayer gamePlayer : this.gamePlayers) {
            if (gamePlayer.isOnline() && !gamePlayer.isSpectator()) {
                return false;
            }
        }
        return true;
    }
    
    public void teleportToSpawn() {
        for (final GamePlayer gamePlayer : this.gamePlayers) {
            if (gamePlayer.isOnline()) {
                gamePlayer.getPlayer().teleport(this.spawnLocation);
            }
        }
    }
    
    public List<GamePlayer> getGamePlayers() {
        return this.gamePlayers;
    }
    
    public TeamColor getTeamColor() {
        return this.teamColor;
    }
    
    public Location getSpawnLocation() {
        return this.spawnLocation;
    }
    
    public Location getWitherLocation() {
        return this.witherLocation;
    }
    
    public GameRegion getRegion() {
        return this.region;
    }
    
    public GameRegion getCastleRegion() {
        return this.castleRegion;
    }
    
    public GameWall getReserveWall() {
        return this.reserveWall;
    }
    
    public int getMaxPlayers() {
        return this.maxPlayers;
    }
    
    public TeamWither getTeamWither() {
        return this.teamWither;
    }
    
    public boolean isWitherDead() {
        return this.witherDead;
    }
    
    public void setWitherDead(final boolean witherDead) {
        this.witherDead = witherDead;
    }
}
